package com.mapper;


import com.dto.UserDTO;
import com.pojo.Users;
import org.apache.ibatis.annotations.*;

import java.util.List;
@Mapper
public interface UsersMapper {
    /**
     * 插入新用户
     * @param user 用户对象
     * @return 受影响的行数
     */
    @Insert("INSERT INTO users (username, password, nickname, avatar_url, bio, email) " +
            "VALUES (#{username}, #{password}, #{nickname}, #{avatarUrl}, #{bio}, #{email})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insertUser(Users user);

    /**
     * 根据ID查询用户
     * @param id 用户ID
     * @return 用户对象
     */
    @Select("SELECT * FROM users WHERE id = #{id}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password"),
            @Result(property = "nickname", column = "nickname"),
            @Result(property = "avatarUrl", column = "avatar_url"),
            @Result(property = "bio", column = "bio"),
            @Result(property = "email", column = "email"),
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "updatedAt", column = "updated_at")
    })
    Users selectUserById(Long id);


    @Select("SELECT id, username, nickname, avatar_url, bio, created_at " +
            "FROM users WHERE id = #{id}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "nickname", column = "nickname"),
            @Result(property = "avatarUrl", column = "avatar_url"),
            @Result(property = "bio", column = "bio"),
            @Result(property = "createdAt", column = "created_at")
    })
    UserDTO selectUserDTOById(Long id);

    /**
     * 根据用户名查询用户
     * @param username 用户名
     * @return 用户对象
     */
    @Select("SELECT * FROM users WHERE username = #{username}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password"),
            @Result(property = "nickname", column = "nickname"),
            @Result(property = "avatarUrl", column = "avatar_url"),
            @Result(property = "bio", column = "bio"),
            @Result(property = "email", column = "email"),
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "updatedAt", column = "updated_at")
    })
    Users selectUserByUsername(String username);

    /**
     * 根据邮箱查询用户
     * @param email 邮箱
     * @return 用户对象
     */
    @Select("SELECT * FROM users WHERE email = #{email}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password"),
            @Result(property = "nickname", column = "nickname"),
            @Result(property = "avatarUrl", column = "avatar_url"),
            @Result(property = "bio", column = "bio"),
            @Result(property = "email", column = "email"),
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "updatedAt", column = "updated_at")
    })
    Users selectUserByEmail(String email);

    /**
     * 更新用户信息
     * @param user 用户对象
     * @return 受影响的行数
     */
    @Update("<script>" +
            "UPDATE users " +
            "<set>" +
            "  <if test='username != null'>username = #{username},</if>" +
            "  <if test='password != null'>password = #{password},</if>" +
            "  <if test='nickname != null'>nickname = #{nickname},</if>" +
            "  <if test='avatarUrl != null'>avatar_url = #{avatarUrl},</if>" +
            "  <if test='bio != null'>bio = #{bio},</if>" +
            "  <if test='email != null'>email = #{email},</if>" +
            "</set>" +
            "WHERE id = #{id}" +
            "</script>")
    int updateUser(Users user);

    /**
     * 更新用户头像
     * @param id 用户ID
     * @param avatarUrl 头像URL
     * @return 受影响的行数
     */
    @Update("UPDATE users SET avatar_url = #{avatarUrl} WHERE id = #{id}")
    int updateAvatar(@Param("id") Long id, @Param("avatarUrl") String avatarUrl);

    /**
     * 更新用户密码
     * @param id 用户ID
     * @param newPassword 新密码
     * @return 受影响的行数
     */
    @Update("UPDATE users SET password = #{newPassword} WHERE id = #{id}")
    int updatePassword(@Param("id") Long id, @Param("newPassword") String newPassword);

    /**
     * 更新用户个人简介
     * @param id 用户ID
     * @param bio 个人简介
     * @return 受影响的行数
     */
    @Update("UPDATE users SET bio = #{bio} WHERE id = #{id}")
    int updateBio(@Param("id") Long id, @Param("bio") String bio);

    /**
     * 删除用户
     * @param id 用户ID
     * @return 受影响的行数
     */
    @Delete("DELETE FROM users WHERE id = #{id}")
    int deleteUser(Long id);

    /**
     * 查询所有用户
     * @return 用户列表
     */
    @Select("SELECT * FROM users")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "nickname", column = "nickname"),
            @Result(property = "avatarUrl", column = "avatar_url"),
            @Result(property = "email", column = "email"),
            @Result(property = "createdAt", column = "created_at")
    })
    List<Users> selectAllUsers();

    /**
     * 检查用户名是否存在
     * @param username 用户名
     * @return 存在返回1，不存在返回0
     */
    @Select("SELECT COUNT(*) FROM users WHERE username = #{username}")
    int checkUsernameExists(String username);

    /**
     * 检查邮箱是否存在
     * @param email 邮箱
     * @return 存在返回1，不存在返回0
     */
    @Select("SELECT COUNT(*) FROM users WHERE email = #{email}")
    int checkEmailExists(String email);

    /**
     * 分页查询用户
     * @param offset 偏移量
     * @param pageSize 每页数量
     * @return 用户列表
     */
    @Select("SELECT * FROM users LIMIT #{offset}, #{pageSize}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "nickname", column = "nickname"),
            @Result(property = "avatarUrl", column = "avatar_url"),
            @Result(property = "email", column = "email"),
            @Result(property = "createdAt", column = "created_at")
    })
    List<Users> selectUsersByPage(@Param("offset") int offset, @Param("pageSize") int pageSize);

    /**
     * 获取用户总数
     * @return 用户总数
     */
    @Select("SELECT COUNT(*) FROM users")
    int countUsers();

}
